<?php
declare (strict_types = 1);

namespace plugin\admin\app\common\service;

use Exception;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;

class sendRequest
{
    private $client;

    public function __construct()
    {
        $this->client = new Client();
    }

    /**
     * 发送 HTTP 请求
     * @param string $method 请求方法（GET 或 POST）
     * @param string $url 请求地址
     * @param array $headers 请求头
     * @param array $params 请求参数
     * @param bool $params_type false是form_params，true是json格式
     * @return array
     * @throws GuzzleException
     */
    public function sendRequest(string $method, string $url, array $headers = [], array $params = [], bool $params_type = false): array
    {
        try {
            $options = [
                'headers' => $headers,
            ];
            // 根据请求方法设置不同的参数
            if ($method === 'GET') {
                $options['query'] = $params;
            } elseif ($method === 'POST') {
                if(!$params_type) {
                    $options['form_params'] = $params;
                } else {
                    $options['json'] = $params;
                }
            }
            // 发送请求
            $response = $this->client->request($method, $url, $options);
            // 返回响应内容
            return [
                'status_code' => $response->getStatusCode(),
                'body' => json_decode($response->getBody()->getContents(), true)
            ];
        } catch (RequestException $e) {
            return [
                'status_code' => $e->getResponse() ? $e->getResponse()->getStatusCode() : 500,
                'error' => $e->getMessage(),
            ];
        }
    }

    /**
     * @throws GuzzleException
     */
    public function jdRequest($params): array
    {
        try {
            $response = $this->client->request('get', $params);
            // 返回响应内容
            return [
                'status_code' => $response->getStatusCode(),
                'body' => json_decode($response->getBody()->getContents(), true)
            ];
        } catch (RequestException $e) {
            return [
                'status_code' => $e->getResponse() ? $e->getResponse()->getStatusCode() : 500,
                'error' => $e->getMessage(),
            ];
        }
    }

    /**
     * @throws GuzzleException
     */
    public function jdRequestPost($url, $params): array
    {
        try {
            $options = [
                'headers' => [
                    'content-type' => 'application/x-www-form-urlencoded',
                    'charset' => 'UTF-8'
                ],
            ];
            $options['form_params'] = $params;
            $response = $this->client->request('POST', $url, $options);
            // 返回响应内容
            return [
                'status_code' => $response->getStatusCode(),
                'body' => json_decode($response->getBody()->getContents(), true)
            ];
        } catch (RequestException $e) {
            return [
                'status_code' => $e->getResponse() ? $e->getResponse()->getStatusCode() : 500,
                'error' => $e->getMessage(),
            ];
        }
    }

}